<?php

//-----------------------------------咨询分类
include_once($config['webroot']."/module/product/includes/plugin_consult_class.php");
$consult=new consult();
if($_POST['act']=='add')
{
	$param = $_POST;
	if(empty($buid)){
		msg($config['weburl']."/login.php");
		return false;
	}
	if(strtolower($param['checkcode']) != strtolower($_SESSION["auth"])){
		msg("?m=product&s=detail&id=$param[product_id]",'验证码不正确');
		return false;
	}
	if(empty($param['question'])){
		msg("?m=product&s=detail&id=$param[product_id]",'咨询内容不能为空');
		return false;
	}
	foreach($param as $key => $val){
		$param[$key] = htmlspecialchars(stripslashes($val));
	}
	$user=get_member_field($buid,'user');
	$provinceid = !empty(getdistrictid($dpid)) ? getdistrictid($dpid) : 0;
	$cityid = !empty(getdistrictid($dcid)) ? getdistrictid($dcid) : 0;
	$areaid = !empty(getdistrictid($daid)) ? getdistrictid($daid) : 0;

	$sql="insert into ".CONSULT." (`catid`,product_id,product_name,member_id,member_name,question,answer,answer_id,question_time,answer_time,status,provinceid,cityid,areaid) values
	 		('$param[catid]','$param[pid]','$param[product_name]','$buid','$user','$param[question]','','0','".time()."','','1','$provinceid','$cityid','$areaid')";
	$db->query($sql);
	$last_id = $db->lastid();
	if($last_id){
		//msg("?m=product&s=detail&id=$param[product_id]",'咨询成功');
		msg("main.php?m=product&s=admin_consult",'咨询成功');
		return false;
	} else{
		msg("?m=product&s=detail&id=$param[product_id]",'咨询失败');
		return false;
	}

}
$id=$_GET["pid"]?$_GET["pid"]*1:NULL;
$catid=$_GET['type']?$_GET['type']*1:NULL;
$key=$_GET['k']?$_GET['k']:NULL;
if(!empty($id) and is_numeric($id))
{
	$tpl->assign("consultcat",$consult->get_consult_cat());
	include_once($config['webroot']."/module/product/includes/plugin_pro_class.php");
	$prodetail=new pro();
	$prode=$prodetail->detail($id);
	$tpl->assign("de",$prode);
	
	if(strlen($prode['catid'])>4)
	{
		if(strlen($prode['catid'])>8)
			$catname[]=substr($prode['catid'],0,-6);
		if(strlen($prode['catid'])>6)
			$catname[]=substr($prode['catid'],0,-4);
		if(strlen($prode['catid'])>4)
			$catname[]=$parcat=substr($prode['catid'],0,-2);
		$catname[]=$prode['catid'];
		$tpl->assign("catname",$catname);
	}

	include_once("includes/page_utf_class.php");
	$page = new Page;
	$page->url=$config['weburl'].'/';
	$page->listRows= 20;
	if(!empty($catid) and is_numeric($catid))
	{
		$ssql = " and catid = '$catid'";
	}
	if(!empty($key))
	{
		$ssql = " and question like '%$key%'";
	}
	$sql="select * from ".CONSULT." where product_id='$id' $ssql order by question_time desc";
	
	if(!$page->__get('totalRows'))
	{
		$db->query($sql);
		$page->totalRows =$db->num_rows();
	}
	$sql.=" limit ".$page->firstRow.", ".$page->listRows;
	//--------------------------------------------------
	$db->query($sql);
	$re['list']=$db->getRows();
	$re['page']=$page->prompt();
	$tpl->assign("re",$re);
}
else
{
	header("Location: 404.php");	
}
$ar1=array('[catname]','[title]','[keyword]','[brand]');
$ar2=array($pcats['cat'],$prode['pname'],$prode['keywords'],$prode['brand']);
$config['title']=str_replace($ar1,$ar2,$config['title3']);
$config['keyword']=str_replace($ar1,$ar2,$config['keyword3']);
$config['description']=str_replace($ar1,$ar2,$config['description3']);
//======================================
$tpl->assign("current","product");
include_once("footer.php");
$out=tplfetch("consult.htm",$flag);
?>